using System;
using System.IO;
using System.Net;
using System.Threading;

namespace RStein.Posterous.API
{
    #if !SILVERLIGHT
    internal class WebClientEx : WebClient, IWebClient
    {
        private Action<WebRequest> m_requestAction;
        private Action<WebResponse> m_responseAction;

        public WebClientEx(Action<WebRequest> requestAction, Action<WebResponse> responseAction)
        {
            init(requestAction, responseAction);
        }

        public WebClientEx()
        {
            init(null, null);
        }

        #region methods
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            m_requestAction(request);
            return request;
        }



        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            return response;
        }
        

        
        private void init(Action<WebRequest> requestAction, Action<WebResponse> responseAction)
        {
            m_requestAction = requestAction ?? delegate
            {
            };
            m_responseAction = responseAction ?? delegate
            {
            };
        }

        #endregion methods

        #region Implementation of IWebClientMarker

        public virtual void SetWebClientActions(Action<WebRequest> requestAction, Action<WebResponse> responseAction)
        {
            init(requestAction, responseAction);
        }

        #endregion
    }

    #endif

}